package nckd.wnqjt.tmc.tda.plugin.form;

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.entity.earlywarn.EarlyWarnContext;
import kd.bos.entity.earlywarn.warn.plugin.IEarlyWarnCustomReceiver;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;

import java.util.ArrayList;
import java.util.List;

public class DepositReceiver implements IEarlyWarnCustomReceiver {
    @Override
    public List<Long> getReceiverIds(EarlyWarnContext earlyWarnContext, DynamicObject[] dynamicObjects) {
        List<Long> receiverIds = new ArrayList<>();
        for (DynamicObject object : dynamicObjects) {
            /**查询对应的定期存款处理单**/
            DynamicObject depositObj = BusinessDataServiceHelper.loadSingleFromCache(object.getPkValue(),"cim_deposit");
            if (depositObj!=null){
                /**存款组织**/
                DynamicObject org = depositObj.getDynamicObject("org");
                /**查询组织财务部长维护单**/
                QFilter[] orgFinMinisterFilter = new QFilter("billstatus", QCP.equals, "C").toArray();
                DynamicObject[] orgFinMinisterObj = BusinessDataServiceHelper.load("nckd_orgfinminister","id",orgFinMinisterFilter,"createtime desc");
                if (orgFinMinisterObj!=null && orgFinMinisterObj.length>0){
                    /**取最新的一条数据**/
                    DynamicObject orgFinMinister = BusinessDataServiceHelper.loadSingle(orgFinMinisterObj[0].getPkValue(), "nckd_orgfinminister");
                    DynamicObjectCollection entryentitys = orgFinMinister.getDynamicObjectCollection("entryentity");
                    for (DynamicObject entryentity:entryentitys) {
                        DynamicObject nckdOrg = entryentity.getDynamicObject("nckd_org");
                        if (org.getString("number").equals(nckdOrg.getString("number"))){
                            DynamicObject finMinister = entryentity.getDynamicObject("nckd_finminister");
                            DynamicObject cashier = entryentity.getDynamicObject("nckd_cashier");
                            receiverIds.add((Long) finMinister.getPkValue());
                            if(finMinister.getPkValue() != cashier.getPkValue()){
                                receiverIds.add((Long) cashier.getPkValue());
                            }
                        }
                    }
                }
            }
        }
        return receiverIds;
    }
}
